#!/bin/bash

# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

changed_cs_files=$(git diff --cached --name-only | grep -E "\.cs$" || true)

echo "Checking changed csharp files for copyright and license statements..."

check_failed=false

for filename in $changed_cs_files; do
  if ! test -f "${filename}"; then continue; fi
  if ! grep -Eqz "// Copyright \(c\) Microsoft Corporation\. All rights reserved\..{1,2}// Licensed under the MIT License\." "${filename}"; then
    # TODO minimal grep calls
    if grep -Fq "// <auto-generated>" "${filename}"; then
      continue
    fi
    echo "Changed file is missing copyright or license:" ${filename}
    check_failed=true
  fi
done

if $check_failed; then
  echo "Should declare copyright and license like:"
  echo "// Copyright (c) Microsoft Corporation. All rights reserved."
  echo "// Licensed under the MIT License."
  exit 1
fi
